﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.Mvc;
using PaperSystem.Models;

namespace PaperSystem.Models
{
    public class LoginRepository
    {
        PaperDatabaseEntities db;
        public LoginRepository()
        {
            db = new PaperDatabaseEntities();
        }

        //验证登录信息
        public bool LoginVerify(LoginInfo info)
        {
            try
            {
                int has;
                if (info.role.Equals("管理员"))
                {
                    int userId = Convert.ToInt32(info.user);
                    has = db.Administrator.Where(a => a.Aid == userId && a.Apassword == info.password).Count();
                    if (has == 1)
                    {
                        return true;
                    }
                }
                else if (info.role.Equals("教师"))
                {
                    string userId = info.user.ToString();
                    has = db.Teacher.Where(t => t.Tid == userId && t.Tpassword == info.password).Count();
                    if (has == 1)
                    {
                        return true;
                    }
                }
                else if (info.role.Equals("学生"))
                {
                    string userId = info.user.ToString();
                    has = db.Student.Where(s => s.Sid == userId && s.Spassword == info.password).Count();
                    if (has == 1)
                    {
                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                return false;
            }
            return false;
        }

        //根据登录信息返回用户名
        public string UserName(LoginInfo info)
        {
            string username = "";
            try
            {
                if (info.role.Equals("管理员"))
                {
                    int userId = Convert.ToInt32(info.user);
                    var tempdata = db.Administrator.Where(a => a.Aid == userId && a.Apassword == info.password).FirstOrDefault();
                    username = tempdata.Aname;
                }
                else if (info.role.Equals("教师"))
                {
                    var tempdata = db.Teacher.Where(t => t.Tid == info.user && t.Tpassword == info.password).FirstOrDefault();
                    username = tempdata.Tname;
                }
                else if (info.role.Equals("学生"))
                {
                    var tempdata = db.Student.Where(s => s.Sid == info.user && s.Spassword == info.password).FirstOrDefault();
                    username = tempdata.Sname;
                }
            }
            catch (Exception e)
            {
                return username;
            }
            return username;
        }

        //创建下拉列表内容
        public SelectList CreateDropdownList()
        {
            List<SelectListItem> listItem = new List<SelectListItem>();
            listItem.Add(new SelectListItem { Text = "教师", Value = "教师" });
            listItem.Add(new SelectListItem { Text = "学生", Value = "学生" });
            listItem.Add(new SelectListItem { Text = "管理员", Value = "管理员" });
            return new SelectList(listItem, "Value", "Text", "");
        }
    }
}
